package com.micro.modual.service.impl;

import com.micro.common.MicroResponseCode;
import com.micro.core.exception.BizException;
import com.micro.core.frame.service.impl.AbstractBasicsServiceImpl;
import com.micro.core.resp.RespJson;
import com.micro.core.resp.RespJsonData;
import com.micro.modual.entity.SystemElement;
import com.micro.modual.entity.SystemResourceAuth;
import com.micro.modual.extension.SystemResourceAuthExtension;
import com.micro.modual.mapper.SystemElementMapper;
import com.micro.modual.mapper.SystemResourceAuthMapper;
import com.micro.modual.model.bo.systemresourceauth.RoleElementBindBO;
import com.micro.modual.model.bo.systemresourceauth.RoleElementFindBO;
import com.micro.modual.model.bo.systemresourceauth.RoleMenuBindBO;
import com.micro.modual.model.bo.systemresourceauth.SystemResourceAuthBO;
import com.micro.modual.model.dto.systemresourceauth.RoleResourceElementDTO;
import com.micro.modual.service.SystemResourceAuthService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
*  <h1>角色菜单关联表  实现</h1>
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:11:10
 */
@Service("systemResourceAuthService")
public class SystemResourceAuthServiceImpl extends AbstractBasicsServiceImpl<SystemResourceAuth,Long> implements SystemResourceAuthService {

    @Autowired
    private SystemResourceAuthMapper systemResourceAuthMapper;

    @Autowired
    private SystemElementMapper systemElementMapper;

    /**
     * <h1>根据角色编号菜单编号查询所拥有的元素信息</h1>
     * @param roleElementFindBO {@link RoleElementFindBO} 角色元素绑定 查询请求对象
     * @return {@link RoleResourceElementDTO} 角色元素绑定
     */
    @Override
    public RespJsonData<List<RoleResourceElementDTO>> findRoleResourceElement(RoleElementFindBO roleElementFindBO) {
        List<String> elementAuthList = systemResourceAuthMapper.findResourceAuthElementList(roleElementFindBO.getRoleCode(),roleElementFindBO.getMenuCode());
        List<SystemElement> systemElementList = systemElementMapper.findSystemElementList(roleElementFindBO.getMenuCode());
        List<RoleResourceElementDTO> roleResourceElementDTOList = SystemResourceAuthExtension.converRoleResourceElementListDTO(elementAuthList,systemElementList);
        return new RespJsonData<List<RoleResourceElementDTO>>(roleResourceElementDTOList,MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>根据角色编号查询所拥有的菜单权限</h2>
     * @param roleCode  角色编号
     * @return
     */
    @Override
    public RespJsonData<List<String>> findRoleResourceMenu(String roleCode) {
        List<String> roleResourceMenu = systemResourceAuthMapper.findRoleResourceMenu(roleCode);
        return new RespJsonData<List<String>>(roleResourceMenu,MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>根据角色编号绑定所拥有的菜单权限</h2>
     * @param roleMenuBindBO {@link RoleMenuBindBO} 角色菜单绑定 请求对象
     * @return
     */
    @Override
    public RespJson bindRoleResourceMenu(RoleMenuBindBO roleMenuBindBO) {
        systemResourceAuthMapper.deleteRoleResourceMenuByRoleCode(roleMenuBindBO.getRoleCode());
        List<SystemResourceAuth> systemResourceAuthList = SystemResourceAuthExtension.converSystemResourceAuthMenu(roleMenuBindBO);
        if(CollectionUtils.isNotEmpty(systemResourceAuthList)){
            systemResourceAuthMapper.insertListKeySelectiveMapper(systemResourceAuthList);
        }
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>角色资源(按钮)相关:角色资源绑定</h2>
     * @param roleElementBO {@link RoleElementBindBO} 角色元素关联请求对象
     * @return
     */
    @Override
    @Transactional(rollbackFor = {Exception.class, BizException.class})
    public RespJson bindRoleResourceElement(RoleElementBindBO roleElementBO) {
        SystemResourceAuth systemResourceAuth = SystemResourceAuthExtension.converSystemResourceAuthElement(roleElementBO);
        systemResourceAuthMapper.insertSelective(systemResourceAuth);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>角色资源(按钮)相关:角色资源移除</h2>
     * @param roleElementBO {@link RoleElementBindBO} 角色元素关联请求对象
     * @return
     */
    @Override
    @Transactional(rollbackFor = {Exception.class, BizException.class})
    public RespJson removeRoleResourceElement(RoleElementBindBO roleElementBO) {
        systemResourceAuthMapper.removeRoleResourceElement(roleElementBO.getRoleCode(),roleElementBO.getElementCode());
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }


}
